package com.gdsdxy.bookshop.service;
import com.gdsdxy.bookshop.constant.Constant;
import com.gdsdxy.bookshop.dao.OItemDtoMapper;
import com.gdsdxy.bookshop.dao.OrderMapper;
import com.gdsdxy.bookshop.dao.UserMapper;
import com.gdsdxy.bookshop.dto.GenerateOrderDto;
import com.gdsdxy.bookshop.dto.OItemDto;
import com.gdsdxy.bookshop.dto.OrderDto;
import com.gdsdxy.bookshop.pojo.Book;
import com.gdsdxy.bookshop.pojo.OItem;
import com.gdsdxy.bookshop.pojo.Order;
import com.gdsdxy.bookshop.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OItemService oItemService;
    @Autowired
    OItemDtoMapper oItemDtoMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    BookService bookService;

    /**
     * 功能描述：在订单表中Orders添加一条记录，返回添加记录的主键
     * @param order 顾客标识
     * @return 返回新增记录的主键ID
     */
    public Integer create(Order order){
        orderMapper.create(order);
        return order.getOrderid();
    }

    /**
     * 功能描述：生成订单
     * @param dtoList
     * @param userid 顾客标识
     * @param
     * @return
     */
    @Transactional
    public OrderDto generate(Integer userid,List<GenerateOrderDto> dtoList) {
        //下面的代码是给Order表添加一条记录
        Order order = new Order();
        order.setUserid(userid); // 用户标识
      //  order.setBookid(bookid); //书籍标识
        order.setAmount(0D);// 默认订单总额
        order.setState(Constant.OrderState.INIT); // 订单状态
        Integer orderid = create(order);//在订单表中新增一条订单记录，返回新增的订单的订单Id
        System.out.println(orderid);


        //下面的代码是给Item表添加多条记录
        Double orderTotalprice = 0D;
        for (GenerateOrderDto generateOrderDto : dtoList) {
            //每趟循环给item表添加一条记录
            Book book = bookService.findById(generateOrderDto.getBookid());//根据商品ID查找商品的详细信息
            OItem oItem = new OItem();
            oItem.setOrderid(orderid);//订单标识
            oItem.setAddressid(generateOrderDto.getAddressid());//地址标识
            oItem.setBookid(generateOrderDto.getBookid());//书籍标识
            oItem.setPrice(oItem.getPrice());//获取商品单价
            oItem.setQuantity(generateOrderDto.getAmount());//单个订单商品数量
//            Oitem.setUnitprice(unitprice);//单个商品的总额
            Double totalprice = generateOrderDto.getAmount() * book.getPrice();//计算购买的全部商品的总金额
            oItem.setTotalprice(totalprice);//每种商品的总额
            oItemService.create(oItem); //在订单详细信息表item中添加记录，扣减商品数量已经在触发器处理
            /* 累加订单总额 */
            orderTotalprice += totalprice;
        }

        /* 更新在order表中添加的订单总额字段 */
        updateAmount(orderid, orderTotalprice);

        return null;
    }



    //将订单的详细信息封装在OrderDto对象中
    public OrderDto detail(Integer userid, Integer orderid){
        OrderDto orderDto=new OrderDto();
        orderDto.setOrderid(orderid);
        orderDto.setUserid(userid);
        //在user表中查找userid=userid的顾客对象
        User user = userMapper.selectById(userid);
        //将查找到的user对象中的username属性值设置为orderDto对象的username属性值
        //orderDto.setUserName(user.getUserName());
        //在order表(订单主表)查找出id=orderid的记录，把记录放在Order对象中
        Order order = orderMapper.selectById(orderid);
        orderDto.setAmount(order.getAmount());
        orderDto.setState(order.getState());
        //通过map对象设置查询条件
        Map<String,Object> map=new HashMap<>();
        map.put("orderid",orderid);//map中的键是数据表的列名，不是实体类的的属性名
        List<OItemDto> oItemDtoList = oItemDtoMapper.selectByMap(map);
        orderDto.setOitemList(oItemDtoList);
        return orderDto;
    }

    private void updateAmount(Integer orderid,Double amount){
        Order order=new Order();
        order.setOrderid(orderid);
        order.setAmount(amount);
        orderMapper.updateById(order);
    }

}
